﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DisplayBoardInfo.Server.Entities;
using DisplayBoardInfo.Network;

namespace DisplayBoardInfo.Server.Forms.Administrator
{
    public partial class Administrator : Form
    {
        //Current session manager
        Session.SessionManager m = null;

        //Current board
        Board.BoardWindow boardWindow;

        //Current boards
        List<Board.Board> boards;

        public Administrator()
        {
            InitializeComponent();

            boards = new List<Board.Board>();

            boardWindow = new Board.BoardWindow();
            boardWindow.FormClosed += new FormClosedEventHandler(boardWindow_FormClosed);
        }
        private void Administrator_Load(object sender, EventArgs e)
        {
            Helpers.Voice.Read("Witaj w programie DisplayBoardInfo");
        }

        private void usersButton_Click(object sender, EventArgs e)
        {
            (new Forms.Administrator.Users()).ShowDialog();
        }

        private void startButton_Click(object sender, EventArgs e)
        {
            Cursor = Cursors.AppStarting;
            try
            {
                if (startButton.CurrentStatus == CustomComponents.StartToolStripButton.Status.Started)
                {   
                    m = new Session.SessionManager();
                    m.UserLogged += new Session.SessionManager.UserLoggedHandler(m_UserLogged);
                    m.UserLogout += new Session.SessionManager.UserLoggedHandler(m_UserLogout);
                    m.Start();
                }
                else if (startButton.CurrentStatus == CustomComponents.StartToolStripButton.Status.Stoped)
                {
                    m.Stop();
                }
            }
            catch (Exception ex)
            {
                Logger.Logger.Message(Logger.LogMessageType.Error, ex.Message);
                startButton.CurrentStatus = CustomComponents.StartToolStripButton.Status.Stoped;
                m.Stop();
                Cursor = Cursors.Arrow;
            }
            Cursor = Cursors.Arrow;
        }

        void m_UserLogout(object sender, Session.SessionEventArgs e)
        {
            try
            {
                this.Invoke((MethodInvoker)delegate()
                {
                    string doctor = e.User.Username;
                    int x = 0;
                    foreach (Board.Board board in new List<Board.Board>(boards))
                    {
                        if (board.Doctor.Username == doctor)
                        {
                            boards.RemoveAt(x);
                        }
                        x++;
                    }
                    boardWindow.Boards = boards;
                    boardsToDisplay1.Boards = boards;
                });
            }
            catch (Exception)
            { 
                //who cares
            }
        }

        void m_UserLogged(object sender, Session.SessionEventArgs e)
        {
            this.Invoke((MethodInvoker)delegate()
            {
                boards.Add(new Board.Board(e.DataManager));
                if (boardButton.Checked) boardWindow.Boards = boards;
                boardsToDisplay1.Boards = boards;
            });
        }

        private void dbSettingsButton_Click(object sender, EventArgs e)
        {
            (new DatabaseSettings()).ShowDialog();
        }

        private void startButton_StatusChanged(object sender, EventArgs e)
        {
            if (startButton.CurrentStatus == CustomComponents.StartToolStripButton.Status.Started)
            {
                dbSettingsButton.Enabled = false;
            }
            else if (startButton.CurrentStatus == CustomComponents.StartToolStripButton.Status.Stoped)
            {
                dbSettingsButton.Enabled = true;
            }
        }

        private void boardButton_Click(object sender, EventArgs e)
        {
            Cursor = Cursors.AppStarting;
            if (!boardButton.Checked)
            {
                boardWindow = new Board.BoardWindow();
                boardWindow.FormClosed += new FormClosedEventHandler(boardWindow_FormClosed);
                boardWindow.Boards = boards;
                boardWindow.DisplayedBoardIndex = boardsToDisplay1.SelectedIndexes;
                boardWindow.DisplayInterval = boardsToDisplay1.DisplayInterval;
                boardButton.Checked = true;
                boardWindow.Show();
            }
            else
            {
                boardWindow.Close();
                boardButton.Checked = false;
            }
            Cursor = Cursors.Arrow;
        }

        void boardWindow_FormClosed(object sender, FormClosedEventArgs e)
        {
            boardButton.Checked = false;
        }

        #region Board preview

        System.Windows.Forms.Integration.ElementHost elementHost = null;

        private void PreparePreview()
        {
            elementHost = new System.Windows.Forms.Integration.ElementHost();
            elementHost.Dock = System.Windows.Forms.DockStyle.Fill;
            elementHost.Location = new System.Drawing.Point(0, 0);
            elementHost.Name = "elementHost";
            elementHost.Size = new System.Drawing.Size(714, 589);
            elementHost.TabIndex = 0;
            elementHost.Text = "elementHost";
            previewPanel.Controls.Add(elementHost);
        }

        private void ShowPreview()
        {
            
        }

        private void HidePreview()
        {
            elementHost.Child = null;
        }

        #endregion

        private void Administrator_FormClosing(object sender, FormClosingEventArgs e)
        {
            m.Stop();
        }

        private void boardsToDisplay1_IndexesChanged(object sender, EventArgs e)
        {
            if (boardButton.Checked) boardWindow.DisplayedBoardIndex = boardsToDisplay1.SelectedIndexes;
        }

        private void boardsToDisplay1_TimeChanged(object sender, EventArgs e)
        {
            if (boardButton.Checked) boardWindow.DisplayInterval = boardsToDisplay1.DisplayInterval;
        }

        private void voiceSettingsButton_Click(object sender, EventArgs e)
        {
            Cursor = Cursors.AppStarting;
            (new VoiceSettings()).ShowDialog();
            Cursor = Cursors.Arrow;
        }
    }
}
